package com.example.myapplication.view;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentChartBinding;
import com.example.myapplication.widgets.chart.RingBean;
import com.necer.painter.CustomInfoPainter;
import com.necer.painter.InnerPainter;
import com.necer.utils.Attrs;

import org.joda.time.LocalDate;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

@FragmentName("图表")
public class ChartFragment extends Fragment {
    private FragmentChartBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentChartBinding.inflate(inflater);
        return mBinding.getRoot();
    }

    private boolean isCustomType = true;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initRingChart();
        initCalendar();

        mBinding.change.setOnClickListener(v -> {
            mBinding.ringChart.clear();
            initRingChart();
        });
        mBinding.changeType.setOnClickListener(v -> {
            Context context = getContext();
            if (context == null) return;
            if (isCustomType) {
                isCustomType = false;
                InnerPainter calendarPainter = new InnerPainter(context, mBinding.calendar);
                calendarPainter.setDefaultCheckedBackground(ContextCompat.getDrawable(context, R.drawable.shape_calendar_sel_bg));
                calendarPainter.setTodayCheckedBackground(ContextCompat.getDrawable(context, R.drawable.shape_calendar_sel_bg));
                calendarPainter.setTodayUnCheckedBackground(ContextCompat.getDrawable(context, R.drawable.shape_calendar_today_unsel_bg));

                Attrs attrs = calendarPainter.getAttrs();
                attrs.showLunar = false;

                mBinding.calendar.setCalendarPainter(calendarPainter);
            } else {
                isCustomType = true;
                CustomInfoPainter painter = new CustomInfoPainter(getContext(), mBinding.calendar);
                painter.setDefaultCheckedBackground(ContextCompat.getDrawable(context, R.drawable.shape_calendar_sel_bg_2));
                painter.setDefaultUnCheckedBackground(ContextCompat.getDrawable(context, R.drawable.shape_calendar_nor_bg_2));
                painter.setTodayCheckedBackground(ContextCompat.getDrawable(context, R.drawable.shape_calendar_sel_bg_2));
                painter.setTodayUnCheckedBackground(ContextCompat.getDrawable(context, R.drawable.shape_calendar_today_unsel_bg_2));

                Attrs attrs = painter.getAttrs();
                attrs.showLunar = true;

                mBinding.calendar.setCalendarPainter(painter);

                Map<String, String> stringMap = new HashMap<>();
                stringMap.put("2024-04-30", "100");
                stringMap.put("2024-05-08", "-100.02");
                stringMap.put("2024-05-16", "102102150.00");
                painter.setInfoMap(stringMap);
            }
        });
    }

    private void initRingChart() {
//        mBinding.ringChart.setTitleLine(false);
        Random random = new Random();
        int count = random.nextInt(10) + 1;
        for (int i = 0; i < count; i++) {
            int c = random.nextInt(10000);
            mBinding.ringChart.addRingBean(new RingBean("很长名字的分类" + i, c));
            Log.i("ChartFragment", "第" + i + "个值：" + c);
        }

        mBinding.total.setText(String.format(Locale.getDefault(), "%.02f", mBinding.ringChart.getTotalValue()));
        mBinding.info.setText("收入");
    }

    private void initCalendar() {
        CustomInfoPainter painter = new CustomInfoPainter(getContext(), mBinding.calendar);
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("2024-04-30", "100");
        stringMap.put("2024-05-08", "-100.02");
        stringMap.put("2024-05-16", "102102150.00");
        painter.setInfoMap(stringMap);
        mBinding.calendar.setCalendarPainter(painter);
        mBinding.calendar.setMonthHoldEnable(true);
        mBinding.calendar.setWeekHoldEnable(true);
        mBinding.calendar.setOnCalendarChangedListener((baseCalendar, year, month, localDate, dateChangeBehavior) -> {
            Log.i("calendar", "select_back: " + localDate);
        });

        mBinding.calendar.setOnCalendarChangedListener((baseCalendar, year, month, localDate, dateChangeBehavior) -> {
            List<LocalDate> currPagerCheckDateList = baseCalendar.getCurrPagerDateList();
            mBinding.currentRange.setText(currPagerCheckDateList.get(0) + " - " +
                    currPagerCheckDateList.get(currPagerCheckDateList.size() - 1));
        });
        mBinding.calendar.setOnCalendarStateChangedListener(calendarState -> {
            List<LocalDate> currPagerCheckDateList = mBinding.calendar.getCurrPagerDateList();
            mBinding.currentRange.setText(currPagerCheckDateList.get(0) + " - " +
                    currPagerCheckDateList.get(currPagerCheckDateList.size() - 1));
        });
        mBinding.calendarStatus.setOnClickListener(v -> {
            ViewGroup.LayoutParams layoutParams = mBinding.calendar.getLayoutParams();
            if (mBinding.calendar.isWeekState()) {
                mBinding.calendar.toMonth();
                mBinding.calendarStatus.setText("收起");

                layoutParams.height = mBinding.calendar.getMonthHeight();
                mBinding.calendar.setLayoutParams(layoutParams);
            } else {
                mBinding.calendar.toWeek();
                mBinding.calendarStatus.setText("展开");

                layoutParams.height = mBinding.calendar.getWeekHeight();
            }
            mBinding.calendar.setLayoutParams(layoutParams);
        });
    }
}
